package com.sharedprint.system.merchant.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharedprint.system.merchant.service.MerchantInfoService;
import com.sharedprint.system.merchant.service.MerchantUserService;

@Controller
@RequestMapping("/api/merchantinfo")
public class MerchantInfoController {
	private static final Logger logger=Logger.getLogger("MerchantInfoController");
	@Autowired
	private MerchantInfoService merchantinfoservice;
	@RequestMapping("/findPageList")
	@ResponseBody
	public Map<String, Object> queryPageList(HttpServletRequest req) {
		String token =req.getHeader("token");
		String userId=req.getHeader("userId");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		String queryMerInfCode=req.getParameter("queryMerInfCode");
		String queryMerInfNameLike=req.getParameter("queryMerInfNameLike");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		logger.info("token=" + token + ",userId=" + userId);
		List<Map<String, Object>> merchantInfoList = new ArrayList<Map<String, Object>>();
		int totalCount = 0;

		resultMap.put("success", "0");
		if (token != null && !"".equals(token.trim()) && userId != null && !"".equals(userId.trim())) {
			if (currentPage == null || "".equals(currentPage.trim())) {
				currentPage = "0";
			}
			if (pageSize == null || "".equals(pageSize.trim())) {
				pageSize = "10";
			}
			Map<String, Object> searchMap = new HashMap<String, Object>();
			int currentPageIndex = Integer.parseInt(currentPage) * Integer.parseInt(pageSize);
			searchMap.put("currentPageIndex", currentPageIndex);
			searchMap.put("pageSize", Integer.parseInt(pageSize));
			searchMap.put("queryMerInfCode", queryMerInfCode);
			searchMap.put("queryMerInfNameLike", queryMerInfNameLike);
			try {
				totalCount = merchantinfoservice.findCount(searchMap);
				if (totalCount > 0) {
				
					merchantInfoList=merchantinfoservice.findPageList(searchMap);
				}
				resultMap.put("currentPage", Integer.parseInt(currentPage) + 1);
				resultMap.put("totalCount", totalCount);
				resultMap.put("merchantInfoList", merchantInfoList);
				resultMap.put("success", "1");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return resultMap;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	private Map<String, Object> delete(HttpServletRequest req)  {
		Map<String, Object> result = new HashMap<String, Object>();
		int  merchant_id = Integer.parseInt(req.getParameter("merchant_id"));
		Map<String, Object> searchMap = new HashMap<String, Object>();
		searchMap.put("merchant_id", merchant_id);
		if (merchantinfoservice.delete(merchant_id)== 1) {
			result.put("success", "1");
		}
		else 
			result.put("success", "0");
		return result;
		
	}
	
}
